package com.blog1.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.blog1.common.exception.blogException;
import com.blog1.common.util.BeanTransUtils;
import com.blog1.common.util.SecurityUtils;
import com.blog1.enums.ResultCodeEnum;
import com.blog1.mapper.BlogInfoMapper;
import com.blog1.mapper.UserInfoMapper;
import com.blog1.pojo.dataobject.BlogInfo;
import com.blog1.pojo.dataobject.UserInfo;
import com.blog1.pojo.request.LoginRequest;
import com.blog1.pojo.response.LoginResult;
import com.blog1.pojo.response.UserInfoResponse;
import com.blog1.service.UserInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service("userInfoServiceImpl")
public class UseInfoServiceImpl implements UserInfoService {
@Autowired
    private UserInfoMapper userInfoMapper;
@Autowired
    private BlogInfoMapper blogInfoMapper;


    @Override
    public LoginResult login(LoginRequest request) {
        if(request==null){
            throw new blogException(ResultCodeEnum.PARAM_ERROR.getCode(),ResultCodeEnum.PARAM_ERROR.getMsg());
        }
        if(request.getUsername()==null||request.getPassword()==null){
            throw new blogException(ResultCodeEnum.PARAM_ERROR.getCode(),ResultCodeEnum.PARAM_ERROR.getMsg());
        }
        LambdaQueryWrapper<UserInfo>wrapper=new QueryWrapper<UserInfo>()
                .lambda().eq(UserInfo::getUsername,request.getUsername());
        List<UserInfo> userInfos = userInfoMapper.selectList(wrapper);

        if(userInfos.size()==0){
            throw new blogException(ResultCodeEnum.UNAUTHORIZED.getCode(),ResultCodeEnum.UNAUTHORIZED.getMsg());
        }
        for (UserInfo userInfo:userInfos) {
            if(SecurityUtils.verify(request.getPassword(),userInfo.getPassword())){
                return BeanTransUtils.trans(userInfo);
            }

        }
throw new blogException(ResultCodeEnum.UNAUTHORIZED.getCode(),ResultCodeEnum.UNAUTHORIZED.getMsg());
    }

    @Override
    public UserInfoResponse getUserInfo(int userId) {
        LambdaQueryWrapper<UserInfo> wrapper=new QueryWrapper<UserInfo>()
                .lambda().eq(UserInfo::getId,userId)
                .eq(UserInfo::getDeleteFlag,0);
        UserInfo userInfo = userInfoMapper.selectOne(wrapper);
        if(userInfo==null){
            throw new blogException(ResultCodeEnum.NOT_FOUND.getCode(),ResultCodeEnum.NOT_FOUND.getMsg());
        }
        return BeanTransUtils.trans2(userInfo);
    }

    @Override
    public UserInfoResponse getAuthorInfo(int blogId) {
        LambdaQueryWrapper<BlogInfo>wrapper=new QueryWrapper<BlogInfo>()
                .lambda().eq(BlogInfo::getDeleteFlag,0)
                .eq(BlogInfo::getId,blogId);
        BlogInfo blogInfo = blogInfoMapper.selectOne(wrapper);
        if(blogInfo==null){
            throw new blogException(ResultCodeEnum.NOT_FOUND.getCode(),ResultCodeEnum.NOT_FOUND.getMsg());
        }
        return getUserInfo(blogInfo.getUserId());
    }


}
